package com.liang.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.JOptionPane;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.pojo.Gorillaceo;
import com.liang.pojo.Pingjia;
import com.liang.pojo.Role;
import com.liang.pojo.Store;
import com.liang.service.PingjiaService;
import com.liang.service.StoreService;

@Controller
public class StoreController {
	@Autowired
	StoreService storeService;
	@Autowired
	PingjiaService pingjiaService;
	
	@RequestMapping("/store/matong")
	public String toMatong() {
		return "matong";
	}
	@RequestMapping("/store/shafa")
	public String toShafa() {
		return "shafa";
	}
	@RequestMapping("/store/store")
	public String toStore(@RequestParam("id")String id,Model model) {
		Store store = storeService.selectStoreById(Integer.parseInt(id));
		model.addAttribute("store", store);
		return "store";
	}
	@RequestMapping("/store/selectAllStore")
	@ResponseBody
	public Object selectAllStore(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
			@RequestParam(value = "pageText", required = false) String pageText) {
		// 这个必须放在查询条件的前面，最好第一行
				PageHelper.startPage(pageNum, 12);
				Map<String, Object> map = new TreeMap<String, Object>();
				if (pageText == null || "".equals(pageText)) {
					List<Store> allStore = storeService.selectAllStore(map);
					PageInfo<Store> pageInfo = new PageInfo<Store>(allStore);
					return pageInfo;
				}
				if (pageText.contains("%")) {
					pageText = pageText.replaceAll("%", "\\\\%");
				}
				map.put("pageText", pageText);
				List<Store> allStore = storeService.selectAllStore(map);
				PageInfo<Store> pageInfo = new PageInfo<Store>(allStore);
				return pageInfo;
	}
	@RequestMapping("/store/selectAllMatong")
	@ResponseBody
	public Object selectAllMatong(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
			@RequestParam(value = "pageText", required = false) String pageText) {
		// 这个必须放在查询条件的前面，最好第一行
		PageHelper.startPage(pageNum, 12);
		Map<String, Object> map = new TreeMap<String, Object>();
		if (pageText == null || "".equals(pageText)) {
			List<Store> allStore = storeService.selectAllMatong(map);
			PageInfo<Store> pageInfo = new PageInfo<Store>(allStore);
			return pageInfo;
		}
		if (pageText.contains("%")) {
			pageText = pageText.replaceAll("%", "\\\\%");
		}
		map.put("pageText", pageText);
		List<Store> allStore = storeService.selectAllMatong(map);
		PageInfo<Store> pageInfo = new PageInfo<Store>(allStore);
		return pageInfo;
	}
	@RequestMapping("/store/selectAllShafa")
	@ResponseBody
	public Object selectAllShafa(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
			@RequestParam(value = "pageText", required = false) String pageText) {
		// 这个必须放在查询条件的前面，最好第一行
		PageHelper.startPage(pageNum, 12);
		Map<String, Object> map = new TreeMap<String, Object>();
		if (pageText == null || "".equals(pageText)) {
			List<Store> allStore = storeService.selectAllShafa(map);
			PageInfo<Store> pageInfo = new PageInfo<Store>(allStore);
			return pageInfo;
		}
		if (pageText.contains("%")) {
			pageText = pageText.replaceAll("%", "\\\\%");
		}
		map.put("pageText", pageText);
		List<Store> allStore = storeService.selectAllShafa(map);
		PageInfo<Store> pageInfo = new PageInfo<Store>(allStore);
		return pageInfo;
	}
	@RequestMapping("/store/selectStoreById")
	public String selectStoreById(@RequestParam("id")int id,Model model) {
		Store store = storeService.selectStoreById(id);
		model.addAttribute("store", store);
		return null;
	}
	@ResponseBody
	@RequestMapping("/store/insertTwoHundredStore")
	public String insertTwoHundredStore() {
		Map<String,Object> map=new HashMap<String,Object>();
		for (int i = 0; i < 200; i++) {
			map.put("storeName", i%2==0?"马桶"+i:"沙发"+i);map.put("price", Math.random()*1000);map.put("number", (int)(Math.random()*100));
			map.put("sales", (int)(Math.random()*100));map.put("cid", i%2==0?1:2);
			int j=storeService.insertTwoHundredStore(map);
			System.out.println(j);
		}
		return "插入两百个数据";
	}
	
	@RequestMapping("/store/toUpdate")
	public String toUpdate(@RequestParam("id") String id, Model model) {
		Store store = storeService.selectStoreById(Integer.parseInt(id));
		model.addAttribute("store", store);
		return "update";
	}

	@RequestMapping("/store/update")
	public String update(Store store, @RequestParam("id") String id, Model model) {
		
		store.setStoreName(store.getStoreName());
		store.setPrice(store.getPrice());
		store.setNumber(store.getNumber());
		store.setSales(store.getSales());
		store.setSid(Integer.parseInt(id));
		storeService.updateStore(store);
		return "matong";
	}

	/*
	 * @RequestMapping("/role/delete") public String
	 * delete(@RequestParam("id")String id) { int
	 * res=JOptionPane.showConfirmDialog(null, "是否继续删除", "是否继续",
	 * JOptionPane.YES_NO_OPTION); if(res==JOptionPane.YES_OPTION){
	 * roleService.deleteRoleById(Integer.parseInt(id)); //点击“是”后执行这个代码块 } else{
	 * System.out.println("选择否后执行的代码"); //点击“否”后执行这个代码块 }
	 * 
	 * return "main"; }
	 */
	@ResponseBody
	@RequestMapping("/store/deleteBatch")
	public Object deleteBatch(Gorillaceo gorillaceo) {System.out.println(gorillaceo);
		int res = JOptionPane.showConfirmDialog(null, "是否继续删除", "是否继续", JOptionPane.YES_NO_OPTION);
		int i = 0;
		if (res == JOptionPane.YES_OPTION) {
			i = storeService.deleteStoreByList(gorillaceo); // 点击“是”后执行这个代码块
			pingjiaService.deletePingjiaBySidList(gorillaceo);
		} else {
			i = -1; // 点击“否”后执行这个代码块 }
		}
		return i;
	}
}
